package com.ssm.user.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ssm.entity.PageResult;
import com.ssm.user.convert.UserConvert;
import com.ssm.user.entity.dto.UserDto;
import com.ssm.user.entity.dto.UserPageDto;
import com.ssm.user.entity.po.UserPo;
import com.ssm.user.mapper.UserMapper;
import com.ssm.user.service.UserService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Override
    public int addUser(UserDto userDto) {
        UserPo userPo = UserConvert.INSTANCE.convertDtoToPo(userDto);
        int count = userMapper.insert(userPo);
        return count;
    }

    @Override
    public int delete(Integer id) {
        return userMapper.deleteById(id);
    }

    @Override
    public PageResult<UserPo> getUserPage(UserPageDto userPageDto) {
        // 首先创建一个Page对象，传入当前页码和页容量(泛型为查询结果的类型)
        IPage<UserPo> userPoPage = new Page<>(userPageDto.getPageIndex(), userPageDto.getPageSize());

        UserPo userPo = UserConvert.INSTANCE.convertPageDtoToPo(userPageDto);
        IPage<UserPo> userPage = userMapper.getUserPage(userPo, userPoPage);

        PageResult<UserPo> pageResult = new PageResult<>();
        pageResult.loadData(userPage);
        return pageResult;
    }

    @Override
    public UserPo queryById(long id) {
        return userMapper.selectById(id);
    }

    @Override
    public Long queryCount(Map<String, Object> conditions) {
        UserPo userPo = new UserPo();
        //可以将conditions内的元素，赋值到userpo对象上
        return userMapper.count(userPo);
    }
}
